<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
  
<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
  
  <!-- INFO INSERT -->

  <insert id="insertIdentityInfo" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    insert into ${prefix}ACT_ID_INFO (ID_, USER_ID_, TYPE_, KEY_, VALUE_, PASSWORD_, PARENT_ID_, REV_)
    values (
      #{id ,jdbcType=VARCHAR},
      #{userId ,jdbcType=VARCHAR},
      #{type ,jdbcType=VARCHAR},
      #{key ,jdbcType=VARCHAR},
      #{value ,jdbcType=VARCHAR},
      #{passwordBytes ,jdbcType=BLOB},
      #{parentId ,jdbcType=VARCHAR},
      1
    )
  </insert>
  
   <insert id="insertIdentityInfo_postgres" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    insert into ${prefix}ACT_ID_INFO (ID_, USER_ID_, TYPE_, KEY_, VALUE_, PASSWORD_, PARENT_ID_, REV_)
    values (
      #{id ,jdbcType=VARCHAR},
      #{userId ,jdbcType=VARCHAR},
      #{type ,jdbcType=VARCHAR},
      #{key ,jdbcType=VARCHAR},
      #{value ,jdbcType=VARCHAR},
      #{passwordBytes ,jdbcType=BINARY},
      #{parentId ,jdbcType=VARCHAR},
      1
    )
  </insert>
  
  <!-- INFO UPDATE -->
  <update id="updateIdentityInfo" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    update ${prefix}ACT_ID_INFO 
    set REV_ = #{revisionNext ,jdbcType=INTEGER},
        VALUE_ = #{value ,jdbcType=VARCHAR},
        PASSWORD_ = #{passwordBytes ,jdbcType=BLOB}
    where ID_ = #{id}
      and REV_ = #{revision}
  </update>
  
  <update id="updateIdentityInfo_postgres" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    update ${prefix}ACT_ID_INFO 
    set REV_ = #{revisionNext ,jdbcType=INTEGER},
        VALUE_ = #{value ,jdbcType=VARCHAR},
        PASSWORD_ = #{passwordBytes ,jdbcType=BINARY}
    where ID_ = #{id}
      and REV_ = #{revision}
  </update>
  
  <!-- INFO DELETE -->

  <delete id="deleteIdentityInfo" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    delete from ${prefix}ACT_ID_INFO 
    where ID_ = #{id} and REV_ = #{revision}
  </delete>

  <!-- INFO RESULTMAP -->

  <resultMap id="identityInfoResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
    <result property="userId" column="USER_ID_" jdbcType="VARCHAR" />
    <result property="type" column="TYPE_" jdbcType="VARCHAR" />
    <result property="key" column="KEY_" jdbcType="VARCHAR" />
    <result property="value" column="VALUE_" jdbcType="VARCHAR" />
    <result property="passwordBytes" column="PASSWORD_" jdbcType="BLOB" />
    <result property="parentId" column="PARENT_ID_" jdbcType="VARCHAR" />
  </resultMap>
  
  <resultMap id="identityInfoResultMap_postgres" type="org.camunda.bpm.engine.impl.persistence.entity.IdentityInfoEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="revision" column="REV_" jdbcType="INTEGER" />
    <result property="userId" column="USER_ID_" jdbcType="VARCHAR" />
    <result property="type" column="TYPE_" jdbcType="VARCHAR" />
    <result property="key" column="KEY_" jdbcType="VARCHAR" />
    <result property="value" column="VALUE_" jdbcType="VARCHAR" />
    <result property="passwordBytes" column="PASSWORD_" jdbcType="BINARY" />
    <result property="parentId" column="PARENT_ID_" jdbcType="VARCHAR" />
  </resultMap>
  
  <!-- INFO SELECT -->

  <select id="selectIdentityInfoById" parameterType="string" resultMap="identityInfoResultMap">
    select * from ${prefix}ACT_ID_INFO where ID_ = #{id,jdbcType=VARCHAR}
  </select>
  
  <select id="selectIdentityInfoById_postgres" parameterType="string" resultMap="identityInfoResultMap_postgres">
    select * from ${prefix}ACT_ID_INFO where ID_ = #{id,jdbcType=VARCHAR}
  </select>
  
  <select id="selectIdentityInfoByUserIdAndKey" parameterType="map" resultMap="identityInfoResultMap">
    select * from ${prefix}ACT_ID_INFO 
    where USER_ID_ = #{userId,jdbcType=VARCHAR}
      and KEY_ =  #{key,jdbcType=VARCHAR}
      and PARENT_ID_ is null
  </select>
  
   <select id="selectIdentityInfoByUserIdAndKey_postgres" parameterType="map" resultMap="identityInfoResultMap_postgres">
    select * from ${prefix}ACT_ID_INFO 
    where USER_ID_ = #{userId,jdbcType=VARCHAR}
      and KEY_ =  #{key,jdbcType=VARCHAR}
      and PARENT_ID_ is null
  </select>
  
  <select id="selectIdentityInfoKeysByUserIdAndType" parameterType="map" resultType="string">
    select KEY_ from ${prefix}ACT_ID_INFO 
    where USER_ID_ = #{parameter.userId,jdbcType=VARCHAR}
      and TYPE_ =  #{parameter.type,jdbcType=VARCHAR}
      and PARENT_ID_ is null
  </select>
  
  <select id="selectIdentityInfoByUserId" parameterType="string" resultMap="identityInfoResultMap">
    select * from ${prefix}ACT_ID_INFO where USER_ID_ = #{parameter,jdbcType=VARCHAR}
  </select>
  
  <select id="selectIdentityInfoByUserId_postgres" parameterType="string" resultMap="identityInfoResultMap_postgres">
    select * from ${prefix}ACT_ID_INFO where USER_ID_ = #{parameter,jdbcType=VARCHAR}
  </select>

  <select id="selectIdentityInfoDetails" parameterType="string" resultMap="identityInfoResultMap">
    select * from ${prefix}ACT_ID_INFO where PARENT_ID_ = #{parameter,jdbcType=VARCHAR}
  </select>

  <select id="selectIdentityInfoDetails_postgres" parameterType="string" resultMap="identityInfoResultMap_postgres">
    select * from ${prefix}ACT_ID_INFO where PARENT_ID_ = #{parameter,jdbcType=VARCHAR}
  </select>
  
  
</mapper>
